package cn.ncu.controller;

import cn.ncu.entity.User;
import cn.ncu.service.UserService;
import cn.ncu.uitils.RandomTools;
import cn.ncu.uitils.SendEmailUtils;
import cn.ncu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SendEmailUtils sendEmailUtils;

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @PostMapping("register")
    private Result register(String code, @RequestBody User user) {
        Result result = new Result();
        if (code != null && code.equals(stringRedisTemplate.opsForValue().get(user.getEmail()))) {
            try {
                userService.register(user);
                result.setFlag(true);//查询成功标签返回true
            } catch (Exception e) {
                result.setMessage("保存失败，请稍后重试！");//查询失败返回失败信息
            }
        }else {
            result.setMessage("验证码不对或已失效！");
        }
        return result;//携带信息的结果集传递到前端
    }

    @PostMapping("verify")
    public Result verifyCode(@RequestBody User user) {
        String code = user.getEmail();
        String codeValue = RandomTools.randomCode();
        String content = "<h3>" + "亲爱的" + user.getEmail() + "您好！</h3>" +
                "<p style='text-align: center; font-size: 24px; font-weight: bold'>您的注册验证码为:" + codeValue + "，五分钟后失效哦！</p>";
        sendEmailUtils.sendNormalEmail("验证邮件", true, content, true, user.getEmail());
        Result result = new Result();
        try {
            stringRedisTemplate.opsForValue().set(code, codeValue, 5, TimeUnit.MINUTES);//5分钟过期
            result.setFlag(true);
        } catch (Exception e) {
            result.setMessage("啊哦，出现了不可控问题！");
        }
        return result;
    }


    /**
     * 用户邮箱登录
     *
     * @param email
     * @param password
     * @return
     */
    @GetMapping("login")
    public Result login(String email, String password) {
        Result result = new Result();
        User user = userService.login(email, password);
        byte[] bytes;
        if (user.getProfile_photo() != null) {
            bytes = Base64Utils.decodeFromString(user.getProfile_photo());
            user.setProfile_photo(bytes.toString());
        }
        if (user != null) {
            result.setFlag(true).setUser(user);
        } else {
            result.setMessage("用户名或密码错误！");
        }
        return result;
    }

    @PostMapping("setting")
    public Result setUser(@RequestBody User user) {
        Result result = new Result();
        System.out.println(user.getProfile_photo());
//        String encode = Base64Utils.encodeToString(user.getProfile_photo().getBytes());
//        user.setProfile_photo(encode);
        try {
//            userService.updateUser(user);
            result.setFlag(true);
        } catch (Exception e) {
            result.setMessage("更新失败，请重试哦！");
        }
        return result;
    }
}
